package com.gx.auth.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.gx.auth.dao.UserInterfaceDAO;
import com.gx.auth.dao.impl.FunctionInterfaceImplDAO;
import com.gx.auth.dao.impl.UserInterfaceImplDAO;
import com.gx.auth.entity.Function;
import com.gx.auth.entity.User;
import com.gx.auth.service.UserInterfaceService;
import com.gx.exception.DataBaseConnectFailedException;
import com.gx.util.DBHplerSQLite;
import com.gx.util.Encryption;


public class UserInterfaceImplService implements UserInterfaceService{
	private static Logger log = Logger.getLogger(LoginInterfaceImplService.class);
	/**
	 * 获取用户权限
	 */
	@Override
	public List<Function> getUserFunction(String uname)  {
		List<Function> ls = new ArrayList<>();
		try {
			log.info("获取数据库连接");
			Connection conn = DBHplerSQLite.getConnection();
			FunctionInterfaceImplDAO fifd = new FunctionInterfaceImplDAO(conn); 
			User user = new User();
			if(uname==null||uname.length()==0){
				log.info("参数为空或空串");
				throw new NullPointerException("username is null or \"\" ");
			}
			else{
				user.setUname(uname);
				ls = fifd.query(user);
			}
		} catch (DataBaseConnectFailedException | SQLException e) {
			log.info(e.getCause(),e);
		}
		

		return ls;
	}
	
	
	/**
	 * 校验用户密码
	 */
	@Override
	public Boolean checkPwd(String uname,String password) {
		Boolean isUpwd= false;
			try {
				String newPwd = Encryption.encodeByMD5(password);
				Connection conn = DBHplerSQLite.getConnection();
				UserInterfaceDAO usd  = new UserInterfaceImplDAO(conn);
				User user  = usd.query(uname);
				if(user!=null){
					isUpwd = user.getUpwd().equals(newPwd);
					return isUpwd;
				}else{
					throw new NullPointerException("对象为空");
				}
			} catch (DataBaseConnectFailedException | SQLException e) {
				log.info(e.getCause(),e);
			}
		
		return isUpwd;
	}

	
}
